<?php
require_once('../../includes/framework_ajax.php');
$examp = $_GET["q"]; //query number
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
$id = $_GET['id'];

switch ($examp) {
	case 1:
		$sql = "select * from tethys.tb_transacao tt
						inner join tethys.tb_modulotransacao tmt on (tt.ci_transacao = tmt.cd_transacao)
						where tmt.cd_modulo = ".Config::MODULO;
			
		$rs = Connection::query($sql);
		$count = $rs->rowCount();

		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}

		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if ($start<0) $start = 0;

		$sql .= " ORDER BY $sidx $sord offset $start limit $limit;"; //LIMIT $start , $limit
		$rs = Connection::query($sql);
		@$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
			
		$i=0;
		while($row = $rs->fetch()){
			$responce->rows[$i]['id'] = $row['ci_transacao'];
			$responce->rows[$i]['cell'] = array($row['ci_transacao'], $row['nm_transacao']);
			$i++;
		}
		echo json_encode($responce);
		break;
}
?>